Public Class View
    Inherits ObjectBase

    Private msName As String
    Private mlOID As Long
    Private mlOwnerID As Long
    Private mlDatabaseID As Long
    Private mlDatabaseRevID As Long
    Private msDesc As String
    Private msRevDate As String = Now

    Private msFieldsHash As String
    Private msTextHash As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal lID As Long)
        MyBase.New(lID)
    End Sub

    Public Sub New(ByVal lID As Long, ByVal lRevID As Long)
        MyBase.New(lID, lRevID)
    End Sub

#Region "Properties"
    Public Property Name() As String
        Get
            Return msName
        End Get
        Set(ByVal value As String)
            msName = value
        End Set
    End Property

    Public Property OID() As Long
        Get
            Return mlOID
        End Get
        Set(ByVal value As Long)
            mlOID = value
        End Set
    End Property

    Public Property OwnerID() As Long
        Get
            Return mlOwnerID
        End Get
        Set(ByVal value As Long)
            mlOwnerID = value
        End Set
    End Property

    Public Property DatabaseID() As Long
        Get
            Return mlDatabaseID
        End Get
        Set(ByVal value As Long)
            mlDatabaseID = value
        End Set
    End Property

    Public Property DatabaseRevID() As Long
        Get
            Return mlDatabaseRevID
        End Get
        Set(ByVal value As Long)
            mlDatabaseRevID = value
        End Set
    End Property

    Public Property RevDate() As String
        Get
            Return msRevDate
        End Get
        Set(ByVal value As String)
            msRevDate = value
        End Set
    End Property

    Public Property Desc() As String
        Get
            Return msDesc
        End Get
        Set(ByVal value As String)
            msDesc = value
        End Set
    End Property

    Public Property FieldsHash() As String
        Get
            Return msFieldsHash
        End Get
        Set(ByVal value As String)
            msFieldsHash = value
        End Set
    End Property

    Public Property TextHash() As String
        Get
            Return msTextHash
        End Get
        Set(ByVal value As String)
            msTextHash = value
        End Set
    End Property
#End Region

#Region "database"
    Public Sub Load()
        Dim oObject As New DBView

        oObject.Load(Me)
    End Sub

    Public Sub Save(Optional ByVal bForceAdd As Boolean = False)
        Dim oObject As New DBView

        If bForceAdd = False AndAlso mbObjectExists = True Then
            oObject.Update(Me)
        Else

            Dim oNextID As New DBNextIDValue("View")
            Dim lNextID As Long = oNextID.NextValue

            Me.ID = lNextID

            If bForceAdd = False Then
                Me.RevID = 0
            End If

            Me.CreateDate = Now
            oObject.Add(Me)
            mbObjectExists = True
            End If
    End Sub

    Public Sub Rev(ByVal sFieldsHash As String, ByVal sTextHash As String)
        Dim oObject As New DBView

        Me.RevDate = Now

        If Me.FieldsHash > "" OrElse Me.TextHash > "" Then
            mlRevID = mlRevID + 1
            Me.FieldsHash = sFieldsHash
            Me.TextHash = sTextHash

            Me.CreateDate = Now
            oObject.Add(Me)
        Else
            Me.FieldsHash = sFieldsHash
            Me.TextHash = sTextHash
            oObject.Update(Me)

        End If

        mbObjectExists = True
    End Sub

    Public Sub ForceRev()
        Dim oObject As New DBTable

        Me.FieldsHash = ""
        Me.TextHash = ""

        Scan()
    End Sub

    Public Sub Delete()
        Dim oObject As New DBView

        oObject.Delete(Me)
        mbObjectExists = False
    End Sub
#End Region

    Public Function Scan() As Boolean
        Dim bViewHasChanged As Boolean = False

        '---Scan text of this view
        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        Dim oServer As New SQLAutoDocLib.Server(oDatabase.ServerID)
        oServer.Load()

        Dim oLogin As New SQLAutoDocLib.Login(mlOwnerID)
        oLogin.Load()

        '---Get list of fields and checksum
        Dim oFieldScanner As New DBScanVFields(oServer, oDatabase, oLogin, Me)
        Dim oFieldList As ArrayList = Nothing
        Dim sFieldsHash As String = ""
        oFieldScanner.Scan(oFieldList, sFieldsHash)

        '---Get text of view and checksum
        Dim oTextScanner As New DBScanVText(oServer, oDatabase, oLogin, Me)
        Dim sViewText As String = ""
        Dim sTextHash As String = ""
        oTextScanner.Scan(sViewText, sTextHash)

        '---If checksum has changed, then rev view
        If sFieldsHash <> msFieldsHash OrElse _
           sTextHash <> msTextHash Then

            bViewHasChanged = True

            '---View has revved
            Me.Rev(sFieldsHash, sTextHash)

            '---Save field list
            For Each oField As VField In oFieldList
                oField.ViewRevID = Me.RevID
                oField.Save()
            Next

            '---Save text
            Dim oVTextFactory As New SQLAutoDocLib.VText_Factory
            oVTextFactory.SaveText(Me, sViewText)
        End If

        Return bViewHasChanged
    End Function
End Class
